package com.shopoo.oauth.app.converter;

import com.shopoo.oauth.domain.entity.Menu;
import com.shopoo.oauth.domain.entity.Role;
import com.shopoo.oauth.domain.entity.User;
import com.shopoo.oauth.domain.valueobject.UserQuery;
import com.shopoo.oauth.dto.clientobject.MenuCO;
import com.shopoo.oauth.dto.clientobject.MenuTree;
import com.shopoo.oauth.dto.clientobject.MenuTreeDTO;
import com.shopoo.oauth.dto.clientobject.MenuTreeResp;
import com.shopoo.oauth.dto.clientobject.MenuTreeSelect;
import com.shopoo.oauth.dto.clientobject.RoleCO;
import com.shopoo.oauth.dto.clientobject.RoleDetailDTO;
import com.shopoo.oauth.dto.clientobject.RoleOption;
import com.shopoo.oauth.dto.clientobject.TokenCO;
import com.shopoo.oauth.dto.clientobject.UserCO;
import com.shopoo.oauth.dto.cqe.MenuAddCmd;
import com.shopoo.oauth.dto.cqe.MenuQuery;
import com.shopoo.oauth.dto.cqe.MenuUpdateCmd;
import com.shopoo.oauth.dto.cqe.RoleAddCmd;
import com.shopoo.oauth.dto.cqe.RoleQuery;
import com.shopoo.oauth.dto.cqe.RoleUpdateCmd;
import com.shopoo.oauth.dto.cqe.UserAddCmd;
import com.shopoo.oauth.dto.cqe.UserQry;
import com.shopoo.oauth.dto.cqe.UserUpdateCmd;
import com.shopoo.oauth.dto.cqe.UserUpdateStatusCmd;
import com.shopoo.oauth.infrastructure.client.dto.TokenResponse;
import com.shopoo.oauth.infrastructure.database.dataobject.OauthMenu;
import com.shopoo.oauth.infrastructure.database.dataobject.OauthRole;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

/**
 * @Description 转换器
 * @Date 2020/10/29 2:36 下午
 * @Author <a href="mailto:android_li@sina.cn">Joe</a>
 **/
@Mapper
public interface AppConverter {

    AppConverter INSTANCE = Mappers.getMapper(AppConverter.class);


    @Mappings({
            @Mapping(source = "menuId", target = "id"),
            @Mapping(source = "type", target = "hidden")
    })
    MenuCO toMenuCO(OauthMenu oauthMenu);

    @Mappings({
            @Mapping(source = "name", target = "title"),
            @Mapping(source = "menuId", target = "key"),
            @Mapping(source = "menuId", target = "value")
    })
    MenuTreeSelect toMenuTreeSelect(OauthMenu oauthMenu);

    @Mappings({
            @Mapping(source = "url", target = "path")
    })
    MenuTreeResp toMenuTreeResp(OauthMenu oauthMenu);

    @Mappings({
            @Mapping(source = "menuId", target = "id"),
            @Mapping(source = "type", target = "hidden")
    })
    MenuCO toMenuCO(Menu menu);

    Menu toMenu(MenuAddCmd menuAddCmd);
    Menu toMenu(MenuUpdateCmd menuUpdateCmd);
    Menu toMenu(MenuQuery menuQuery);

    MenuTreeDTO toMenuTreeDTO(Menu menu);
    MenuTree toMenuTree(Menu menu);

    @Mappings({
            @Mapping(source = "roleId", target = "value"),
            @Mapping(source = "name", target = "label"),
    })
    RoleOption toRoleOption(Role role);

    Role toRole(RoleAddCmd roleAddCmd);
    Role toRole(RoleUpdateCmd roleUpdateCmd);
    OauthRole toOauthRole(RoleAddCmd roleAddCmd);
    Role toRole(RoleQuery roleQuery);
    RoleCO toRoleCO(Role role);

    RoleDetailDTO toRoleDetailDTO(Role role);

    UserQuery toUserQuery(UserQry userQry);

    User toUser(UserAddCmd userAddCmd);
    User toUser(UserUpdateStatusCmd userUpdateStatusCmd);
    User toUser(UserUpdateCmd userUpdateCmd);

    UserCO toUserCO(User user);

    @Mappings({
            @Mapping(source = "access_token", target = "accessToken"),
            @Mapping(source = "token_type", target = "tokenType"),
            @Mapping(source = "refresh_token", target = "refreshToken"),
            @Mapping(source = "error_description", target = "errorDescription"),
            @Mapping(source = "expires_in", target = "expiresIn"),
    })
    TokenCO toTokenCO(TokenResponse tokenResponse);
}
